<?php
    if(!defined('LT2N_ADMIN')){
        die('bạn không có quyền truy cập');
    }
    $db = new KiemKeTaiSan();
    $module['name'] = $_GET["mod"];
    if($_GET['mod'] == 'kiemketaisan'){


        $act = $_GET['act'];
        if($act == '' || $act == 'list'){
            $act = 'list';

            $sql = "SELECT a.*, b.tentaisan  FROM tscd_kiemketaisan a, tscd_taisancodinh b WHERE a.taisan_id = b.taisan_id ORDER BY a.ngay_kiem_ke";


            $db->setSql($sql);

            $db->query();

            $arr = $db->fetchArray();
            require(LT2N_VIEW_PATH.$module["name"]."/".$act.".php");

        } elseif($act == 'add'){

            initalize_post_var('so_luong','so_luong_kiem_ke','tinh_trang_kiem_ke','dang_su_dung','hu_hong_thanh_ly','hu_hong_sua_chua','mat','khong_nhu_cau_sd','ghi_chu');
            $taisan_info = $db->select(LT2N_PREFIX.'_taisancodinh',array('taisan_id'=>$_GET['id']));
            $soluong_ss = $taisan_info[0]['so_luong'];

            if(isset($_SESSION['permission']) && $_SESSION['permission'] != PERMISSION_USER ){
                if(isset($_POST['btnAdd'])){
                    $soluong_kk         = $_POST['so_luong_kiem_ke'];

                    $soluong_chenhlech = $soluong_kk - $soluong_ss;

                    if($soluong_chenhlech == 0){
                        $thua = 0;
                        $thieu = 0;
                    } elseif($soluong_chenhlech > 0){
                        $thua = $soluong_chenhlech;
                        $thieu = 0;
                    } elseif ($soluong_chenhlech < 0) {
                        $thua = 0;
                        $thieu = -1 * $soluong_chenhlech ;
                    }

                    $dang_su_dung       = $_POST['dang_su_dung'];
                    $hu_hong_thanh_ly   = $_POST['hu_hong_thanh_ly'];
                    $hu_hong_sua_chua   = $_POST['hu_hong_sua_chua'];
                    $mat                = $_POST['mat'];
                    $khong_nhu_cau_sd   = $_POST['khong_nhu_cau_sd'];
                    $ghi_chu            = $_POST['ghi_chu'];

                    $list = array(
                                'taisan_id'=> $_GET['id'] ,
                                'so_luong_kiem_ke '=> $soluong_kk ,
                                'thua' => $thua ,
                                'thieu' => $thieu ,
                                'dang_su_dung'=>$dang_su_dung,
                                'hu_hong_thanh_ly'=> $hu_hong_thanh_ly ,
                                'hu_hong_sua_chua' => $hu_hong_sua_chua,
                                'mat'=>$mat,
                                'khong_nhu_cau_sd'=>$khong_nhu_cau_sd,
                                'ghi_chu'=>$ghi_chu,
                                'ngay_kiem_ke'=> nvs_createDate(date('d-m-Y h:i'), 'dd-mm-yyyy h:i')

                    );



                    $db->insert(LT2N_PREFIX.'_kiemketaisan',$list);
                    if($db->checkQuery() == 1){
                        //$success[] = '<div class="success" >Đã thêm thành công.</div>';
                        header("Location: index.php?mod=kiemketaisan&act=list&success=".SUCCESS_ADD);
                    } else {
                        $errors[] = '<div class="error" >Thêm kiểm kê mới thất bại.</div>';
                    }


                }

                require(LT2N_VIEW_PATH.$module["name"]."/".$act.".php");

            } else {
                header('location: index.php?mod=kiemketaisan&act=list&err='.ERR_NOT_PERMISSION);
            }


        }elseif($act == 'edit') {

            if(isset($_SESSION['permission']) && $_SESSION['permission'] != PERMISSION_USER ){


                    $id_taisan = $db->select(LT2N_PREFIX.'_kiemketaisan',array('kiemketaisan_id'=>$_GET['id']));



                    $taisan_info = $db->select(LT2N_PREFIX.'_taisancodinh',array('taisan_id'=>$id_taisan[0]['taisan_id']));
                    $soluong_ss = $taisan_info[0]['so_luong'];


                    if(isset($_POST['btnUpdate'])){
                        $soluong_kk         = $_POST['so_luong_kiem_ke'];

                        $soluong_chenhlech = $soluong_kk - $soluong_ss;

                        if($soluong_chenhlech == 0){
                            $thua = 0;
                            $thieu = 0;
                        } elseif($soluong_chenhlech > 0){
                            $thua = $soluong_chenhlech;
                            $thieu = 0;
                        } elseif ($soluong_chenhlech < 0) {
                            $thua = 0;
                            $thieu = -1 * $soluong_chenhlech ;
                        }

                        $dang_su_dung       = $_POST['dang_su_dung'];
                        $hu_hong_thanh_ly   = $_POST['hu_hong_thanh_ly'];
                        $hu_hong_sua_chua   = $_POST['hu_hong_sua_chua'];
                        $mat                = $_POST['mat'];
                        $khong_nhu_cau_sd   = $_POST['khong_nhu_cau_sd'];
                        $ghi_chu            = $_POST['ghi_chu'];

                        $list = array(
                                    'taisan_id'=> $id_taisan[0]['taisan_id'] ,
                                    'so_luong_kiem_ke '=> $soluong_kk ,
                                    'thua' => $thua ,
                                    'thieu' => $thieu ,
                                    'dang_su_dung'=>$dang_su_dung,
                                    'hu_hong_thanh_ly'=> $hu_hong_thanh_ly ,
                                    'hu_hong_sua_chua' => $hu_hong_sua_chua,
                                    'mat'=>$mat,
                                    'khong_nhu_cau_sd'=>$khong_nhu_cau_sd,
                                    'ghi_chu'=>$ghi_chu,
                                    'ngay_kiem_ke'=> nvs_createDate(date('d-m-Y h:i'), 'dd-mm-yyyy h:i')

                        );

                        $key = "`kiemketaisan_id` = ". $_GET['id'];


                        $db->update(LT2N_PREFIX.'_kiemketaisan',$list,$key);
                        if($db->checkQuery() == 1){
                            //$success[] = '<div class="success" >Đã thêm thành công.</div>';
                            header("Location: index.php?mod=kiemketaisan&act=list&success=".SUCCESS_EDIT);
                        } else {
                            $errors[] = '<div class="error" >Cập nhật thông tin tài sản thất bại.</div>';
                        }


                    }



                    $arr = $db->select(LT2N_PREFIX.'_kiemketaisan',array('kiemketaisan_id'=>$_GET['id']));
                    require(LT2N_VIEW_PATH.$module["name"]."/".$act.".php");

                } else {
                    header('location: index.php?mod=taisan&act=list&err='.ERR_NOT_PERMISSION);
                }
        } elseif($act == 'del' ){

            if(isset($_SESSION['permission']) && $_SESSION['permission'] == PERMISSION_ADMIN ){
                $db->delete(LT2N_PREFIX.'_taisancodinh',array('kiemketaisan_id'=>$_GET['id']));
                header('location: index.php?mod=kiemketaisan&act=list&success='.SUCCESS_DELETE);

            } else {
                header('location: index.php?mod=kiemketaisan&act=list&err='.ERR_NOT_PERMISSION);
            }


        }


    }
?>